<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {

	function __construct()
	{
		parent::__construct();
			$this->view_data['base_url'] = base_url();
			$this->load->model('User_model');
	}
	function user()
	{	
		parent::Controller();			
	}

	function index()
	{
		$this->register();
	}
	
	function doctrine()
	{
		$this->load->library('doctrine');
	$em = $this->doctrine->em;
	
	}
	
	function login()
	{
		if($this->form_validation->run() == FALSE)
		{
			$this->load->view('welcome_message');
		}
		else
		{
			echo 'aa';
		}
	}
	
	function register()
	{
		
		$this->form_validation->set_rules('username', 'Username', 'trim|required|alpha_numeric|min_lenght[6]|xss_clean|strtolower|callback_username_not_exists');
		$this->form_validation->set_rules('name', 'Name', 'trim|required|alpha_numeric|min_lenght[6]|xss_clean');
		$this->form_validation->set_rules('email', 'Email adress', 'trim|required|min_lenght[6]|xss_clean|valid_email');
		$this->form_validation->set_rules('password', 'Password', 'trim|required|alpha_numeric|min_lenght[6]|xss_clean');
		$this->form_validation->set_rules('password_conf', 'Password confirmation', 'trim|required|alpha_numeric|min_lenght[6]|xss_clean|matches[password]');
		
		
		if($this->form_validation->run() == FALSE)
		{
		$this->load->view('view_register', $this->view_data);
		}
		else 
		{
			$username = $this->input->post('username');
			$name = $this->input->post('name');
			$email = $this->input->post('email');
			$password = $this->input->post('password');
			
			$activation_code = $this->_random_string(10);
			
			echo $username;
			
			$this->User_model->register_user($username, $password, $name, $email, $activation_code);
			
			
			$this->email->from('me@localhost.com', 'Ai');
			$this->email->to($email);
			$this->email->subject('registracijos patvirtinimas');
			$this->email->message('Spausk, kad testi:' .anchor('http://127.0.0.1/index.php/user/register_confirm/' .$activation_code, 'confirm registration'));
			echo 'Spausk, kad testi:' .anchor('http://127.0.0.1/index.php/user/register_confirm/' .$activation_code, 'confirm registration');
			
			//$this->email->send();
			
		}
		
	}
	
	function register_confirm()
	{
		$registration_code = $this->uri->segment(3);
		
		if($registration_code == '')
		{
			echo 'error no registration code in URL';
			exit();
		}
		
		$registration_confirmed = $this->User_model->confirm_registration($registration_code);
		
			if($registration_confirmed)
			{
				echo 'You have succsefully registered';
			}
			else
			{
				echo 'Nepavyko registracija, nerasta iraso sitam aktyvacijos kodui';
			}
	}
	
	function username_not_exists($username)
	{
		$this->form_validation->set_message('username_not_exists', 'jau yra, ivesk kita');
		if($this->User_model->check_exists_username($username))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	function _random_string($lenght)
	{
		$len = $lenght;
		$base = 'ABCDEFGHIJKLMNPQRSTWXYZabcdefghijklmnpqrstwxyz123456789';
		$max = strlen($base)-1;
		$activatecode = '';
		mt_srand((double)microtime()*1000000);
		while (strlen($activatecode) < $len)
			$activatecode.=$base{mt_rand(0, $max)};
			
		return $activatecode;
	}
 
}